﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.IO;

namespace AliyunMobileTestPlatform.Models
{
    public class AppsModel
    {
        Dictionary<string,string> Apps { set; get; }
        string Revision { set; get; }

        public AppsModel(string[] AppDirectories,string revision) 
        {
            Apps = new Dictionary<string,string>();
            foreach (string appDictionary in AppDirectories) 
            {
                string[] arr = appDictionary.Split('\\');
                Apps.Add(arr[arr.Length - 1], appDictionary);
                Revision = revision;
            }
        }

        public string ToHtml() 
        {
            StringBuilder sb = new StringBuilder();
            int count = 0;
            sb.Append("<table class=\"table table-bordered table-striped\">");
                sb.Append("<thead>");
                    sb.Append("<tr>");
                        sb.Append("<th style='width:50%;word-break:break-all'>").Append("AppName").Append("</th>");
                        sb.Append("<th style='width:50%;word-break:break-all'>").Append("</th>");
                    sb.Append("</tr>");
                sb.Append("</thead>");
                sb.Append("<tbody>");
                foreach (string app in Apps.Keys)
                {                 
                    if (count == 0) 
                    {
                        sb.Append("<tr>");
                    }
                    else if (count % 2 == 0)
                    {
                        sb.Append("</tr>").Append("<tr>");
                    }
                    sb.Append("<td>")
                        //.Append("<a href='#' onclick=\"$.get('GetCoverage/")
                        .Append("<a href='#' onclick=\"onAppCoverageClick('")
                        .Append(EncodeString(Apps[app]))
                        .Append("/")
                        .Append(Revision)
                        .Append("')\">")
                        .Append(app).Append("</a>")
                        .Append("<img style=\"margin:10px;width:20px;height:20px;cursor:pointer\" src=\"..\\..\\images\\history.gif\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"点击查看历史曲线\" onclick=\"getHistory('")
                        .Append(EncodeString(Apps[app]))
                        .Append("')\" />")                       
                        .Append("</td>");
                    count++;
                }
                        //补齐TD
                        if (count % 2 != 0)
                        {
                            sb.Append("<td></td>");
                        }
                    sb.Append("</tr>");
                sb.Append("</tbody>");
            sb.Append("</table>");
            return sb.ToString();

        }

        private string EncodeString(string str) 
        {
            return System.Web.HttpUtility.UrlEncode(Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(str)));
        }
    }
}